package com.imon.web.codesphere.resources;

import java.util.Collection;
import java.util.HashMap;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.imon.web.codesphere.data.FileSource;

@Path("/revisions")
@Component
public class RevisionsResource extends ResourceBase {

	private static final Log LOG = LogFactory.getLog(RevisionsResource.class);

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAllRevisions(@QueryParam("path") final String path) {
		LOG.debug(String.format("getAllRevisions(%s)", path));

		if (StringUtils.isEmpty(path)) {
			return Response.status(Status.BAD_REQUEST).build();
		}

		final Collection<FileSource> fileSources = getDatabase().retrieveFields(FileSource.class,
				new HashMap<String, Object>() {
					{
						put("fileUrl =", path);
					}
				}, "-revision", "revision");

		return Response.ok(getJsonParser().getJson(fileSources)).build();
	}
}
